//
//  SleepQualityScoreView.swift
//  MoyoungHome
//
//  Created by 李然 on 2022/9/27.
//

import UIKit
import MYModule

import RxSwift
import RxCocoa


class SleepQualityScoreCell: SleepBaseCell {
    
    override func makeUI() {
        super.makeUI()
        
        self.backgroundColor = UIColor.background
        
        bgView.addSubview(titleLabel)
        bgView.addSubview(scoreUnitLabel)
        bgView.addSubview(scoreLabel)
        bgView.addSubview(scoreProgressView)
        bgView.addSubview(explainView)
        
        titleLabel.snp.makeConstraints { make in
            make.leading.top.equalTo(26)
        }
        
        scoreUnitLabel.snp.makeConstraints { make in
            
            make.leading.equalTo(scoreLabel.snp.trailing)
            make.bottom.equalTo(scoreLabel.snp.bottom).offset(-10)
            
        }
        
        scoreLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(24)
            make.centerX.equalToSuperview()
        }
        
    
        scoreProgressView.snp.makeConstraints { make in
            make.top.equalTo(scoreLabel.snp.bottom).offset(20)
            make.leading.equalTo(26)
            make.trailing.equalTo(-26)
        }
        
        explainView.snp.makeConstraints { make in
            make.top.equalTo(scoreProgressView.snp.bottom)
            make.leading.equalToSuperview()
            make.trailing.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    override func bind(to cellType: SleepViewModel.CellType) {
        guard let qualityScore = cellType.viewModel as? BehaviorRelay<Int> else { return }
        
        qualityScore.subscribe(onNext:{[weak self]  in
           
            
            self?.scoreProgressView.score = $0
            self?.scoreLabel.text = $0.normalTitle
            
        }).disposed(by: rx.disposeBag)

        
    }
    
    

    lazy var titleLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 16)
        label.textColor = UIColor.assist_000000
        label.text = "sleep_quality_upper".language
        return label
    }()
    
    lazy var scoreLabel: Label = {
        let label = Label()
        label.font = Fonts.boldSystemFont(ofSize: 44)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var scoreUnitLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 12)
        label.textColor = UIColor.assist_222222
        label.text = "unit_point".language
        return label
    }()
    
    lazy var scoreProgressView: ScoreProgressView = {
        let view = ScoreProgressView()
        return view
    }()
    
    lazy var explainView: ExPlainView = {
        let view = ExPlainView()
        view.backgroundColor = UIColor.background
        view.updateData(textColor: UIColor.Sleep.theme,
                        title: "sleep_quality".language,
                        explain: "sleep_sleep_quality_description".language,
                        explainTextColor:UIColor.Sleep.theme.withAlphaComponent(0.8),
                        bgColor: UIColor.Sleep.theme)
        return view
    }()

}

class ScoreProgressView: View {
    
   
    var markImage = UIImage(named: "ic_sleep_quality_score_progress")
    
    lazy var markView: ImageView = {
        let view = ImageView()
        view.image = markImage
        return view
    }()
    
    var score:Int = 0 {
        didSet {
            
            markView.isHidden = score == 0
            markImageLayout(score: score)
            self.setNeedsDisplay()
            
        }
    }
    
   let progressColorsView = ProgressColorsView()
    
    override func makeUI() {
        super.makeUI()
        
        self.addSubview(progressColorsView)
        progressColorsView.addSubview(markView)
        
        progressColorsView.clipsToBounds = true
        progressColorsView.cornerRadius = 9.0
        
        markView.snp.makeConstraints { make in
            make.centerY.equalTo(progressColorsView.snp.centerY)
            make.left.equalTo(progressColorsView.snp.left).offset(score)
        }
        
        progressColorsView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(18)
        }
        
        let labelStackView = StackView()
        labelStackView.axis = .horizontal
        labelStackView.distribution = .fillEqually
        
        let strings = ["sleep_poor".language,
                       "sleep_medium".language,
                       "sleep_good".language,
                       "sleep_excellent".language]
        for string in strings {
            let label = Label()
            label.textColor = UIColor.assist_000000
            label.font = Fonts.systemFont(ofSize: 10)
            label.text = string
            label.textAlignment = .center
            labelStackView.addArrangedSubview(label)
        }
        self.addSubview(labelStackView)
        labelStackView.snp.makeConstraints { make in
            make.leading.trailing.equalTo(progressColorsView)
            make.top.equalTo(progressColorsView.snp.bottom).offset(8)
            make.bottom.equalToSuperview()
        }
        
        let scores = ["60","75","90"]
        let scoreStackView = StackView()
        scoreStackView.axis = .horizontal
        scoreStackView.distribution = .fillEqually
        for score in scores {
            let label = Label()
            label.textColor = UIColor.assist_D0D0D0
            label.font = Fonts.systemFont(ofSize: 10)
            label.text = score
            label.textAlignment = .center
            scoreStackView.addArrangedSubview(label)
        }
        self.addSubview(scoreStackView)
        scoreStackView.snp.makeConstraints { make in
            make.center.equalTo(labelStackView)
            make.width.equalToSuperview().multipliedBy(0.75)
        }
    }
    
    func bind(to: ViewModel)  {
        
    }
    
    func markImageLayout(score: Int) {
        
        self.layoutIfNeeded()
        markView.layoutIfNeeded()
        
        let rate = self.frame.width / 4.0
        let quality = CGFloat(score)
        
        var left: CGFloat = 0
        
        if quality == 0 {
            left = 0
           
        } else if quality < 60 {
            left = (rate/60.0) * quality
           
        } else if quality < 75 {
            left = (rate/15.0) * (quality-60) + rate
           
        } else if quality < 90 {
            left = (rate/15.0) * (quality-75) + (rate*2)
          
        } else {
            left = (rate/10.0) * (quality-90) + (rate*3)
        }
        
        if left <= markView.frame.width/2.0 {
            left = 0
        } else if left > self.frame.width - markView.frame.width {
            
            left = self.frame.width - markView.frame.width
        } else {
            
            left = left - markView.frame.width/2.0
            
        }
        
        
        markView.snp.updateConstraints { make in

            if left == 0 {
                make.left.equalToSuperview()
            } else {
                make.left.equalToSuperview().offset(left - 2)
            }

        }
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        markImageLayout(score: score)
    }
    
    
    class ProgressColorsView: View {
        
        private let colors = UIColor.Sleep.assist_score
        private let locations: [NSNumber] = [0.0, 0.5, 1.0]
        
        let colorsLayers = CAGradientLayer()
        
        override func makeUI() {
            super.makeUI()
            
           
            
            colorsLayers.colors = colors.map{ $0.cgColor }
            colorsLayers.locations = locations
            colorsLayers.startPoint = CGPoint(x: 0.0, y: 0.0)
            colorsLayers.endPoint = CGPoint(x: 1.0, y: 0.0)
            layer.addSublayer(colorsLayers)
        }
        
        override func layoutSubviews() {
            super.layoutSubviews()
            
            colorsLayers.frame = self.bounds
        }
        
        
    }
}
